﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CommTestNFW.ImageCode
{
    public class GraphicsManagement : IDisposable
    {
        private bool disposedValue;

        private GraphicsManagement()
        {
        }

        public Graphics Graphics { get; private set; }

        public Pen Pen { get; private set; }

        public Brush Brush { get; private set; }
        public Brush BrushWhite { get; private set;}
        public Brush BrushRed { get; private set; }
        public Brush BrushGreen { get; private set; }

        public Font Font { get; set; }


        public static GraphicsManagement Build(Image image)
        {
            var gm = new GraphicsManagement();

            gm.Graphics = Graphics.FromImage(image);

            gm.Pen = new Pen(Color.Black);

            gm.Brush = gm.Pen.Brush;

            gm.BrushWhite = Brushes.White;
            gm.BrushRed = Brushes.Red;
            gm.BrushGreen = Brushes.DarkGreen;
            //gm.BrushRed = new SolidBrush(Color.Red);
            //m.BrushGreen = new SolidBrush(Color.DarkGreen);

            gm.Font = new Font("微软雅黑", 15F);

            return gm;
        }

        public GraphicsManagement SetColor(Color color)
        {
            Pen.Color = color;
            Brush = Pen.Brush;
            return this;
        }

        public GraphicsManagement SetFont(Font font)
        {
            Font = font;
            return this;
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: 释放托管状态(托管对象)
                    Graphics.Dispose();
                    Pen.Dispose();
                    //Brush.Dispose();
                    //BrushRed.Dispose();
                    //BrushGreen.Dispose();
                }

                Graphics = null;
                Pen = null;
                Brush = null;
                BrushRed = null;
                BrushGreen = null;

                // TODO: 释放未托管的资源(未托管的对象)并替代终结器
                // TODO: 将大型字段设置为 null
                disposedValue = true;
            }
        }

        // // TODO: 仅当“Dispose(bool disposing)”拥有用于释放未托管资源的代码时才替代终结器
        // ~GraphicsManagement()
        // {
        //     // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
        //     Dispose(disposing: false);
        // }

        public void Dispose()
        {
            // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
            Dispose(disposing: true);
            GC.SuppressFinalize(this);
        }
    }
}
